/*
* 数组的常用方法
*/

// let  arr1 = []
// let  arr2 = new Array(10)

/***************************
 ************** 1填充********
 ***************************/ 
// arr2.fill(123);//[123, 123, 123, 123, 123, 123, 123, 123, 123, 123]


// console.log("arr1",arr1);
// console.log("arr2",arr2);













/***************************
 ************** 2 遍历********
 ***************************/ 


let arr3 = [100, 200, 300, 400]
// for(let i = 0 ; i< arr3.length ;i++){ 
//   const item = arr3[i]; 
//   console.log(item);
// } 
// arr3.forEach(function(item , index){
//     console.log(item);
// })

//将回调函数 替换成  箭头函数 
// arr3.forEach((item , index)=>{
//     console.log(index+":"+item);
// })

//代码块中只有一句话  省略大括号
// arr3.forEach((item , index)=> console.log(item) )

//最终的写法
// arr3.forEach(       item=> console.log(item)  )








/***************************
 **************3 过滤********
 ***************************/ 
let users = [

    { name: '张三', age: 18, adddress: { province: 'HLJ', city: 'HRB1' } },
    { name: '李四', age: 28, adddress: { province: 'LN', city: 'HRB2' } },
    { name: '王五', age: 38, adddress: { province: 'ZJ', city: 'HRB3' } },
    { name: '赵六', age: 48, adddress: { province: 'SC', city: 'HRB4' } },
]


// let result = [];

// for (let i = 0; i < users.length; i++) {
//     const user = users[i];
//     if(user.age > 30 ){
//         result.push(user)
//     }
// }



// let result = users.filter(function(user){
//     return user.age < 20 ;
// })

//修改成箭头函数 1 
// let result = users.filter((user)=>{
//     return user.age < 20 ;
// })

let result = users.filter(user=> user.age < 20  )

console.log("result: ",result);








/***************************
 **************4 Map    ********
 ***************************/ 

//  let  ages = [18,26,33]
// // let newAages = ages.map(currentValue=> currentValue * 2);
// let newAages = ages.map(currentValue=> currentValue + 2);

// console.log("ages: " , ages);
// console.log("newAages: " , newAages);
// let stus = [

//     { name: '张三', age: 18, adddress: { province: 'HLJ', city: 'HRB1' } },
//     { name: '李四', age: 28, adddress: { province: 'LN', city: 'HRB2' } },
//     { name: '王五', age: 38, adddress: { province: 'ZJ', city: 'HRB3' } },
//     { name: '赵六', age: 48, adddress: { province: 'SC', city: 'HRB4' } },
// ]
// let newStus = stus.map(stu=> stu.age + 2);
// console.log("stus: " , stus);
// console.log("newStus: " , newStus);


// let names = stus.map(  stu=>  stu.name          )
// console.log("names: " , names);
 
// let names = stus.map(  stu=>  { return {name:stu.name  ,  age:100}  }          )
// console.log("names: " , names);





/***************************
 **************5 Reduce    ********
 ***************************/ 

//  let  nums = [1,2,3,4]

// //  let sum = 0;
// //  for(let i = 0 ; i< nums.length ;i++){ 
// //    const num = nums[i]; 
// //    sum += num
// //  } 
// //  console.log("sum",sum);
 

// // nums.reduce(   (sum , num)=>{
// //     return sum+num
// // })

// //简写
// let total = nums.reduce(   (sum , num)=>  sum + num)
// console.log("total,",total);




/***************************
 **************6 some find    ********
 ***************************/  
let numArray = [10,20,13,15,17]
// let numArray = [10,20]
let success = numArray.some(  (num)=> num%2!=0   )
console.log("success ",success);

let item = numArray.find(  (num)=> num%2!=0   )
console.log("item ",item);
